﻿/*
 * @author: wizardc
 */

using UnityEngine;

namespace Dou.Utils
{
    /// <summary>
    /// 2D贴图工具
    /// </summary>
    public static class Texture2DUtil
    {
        /// <summary>
        /// 对摄像机进行截图
        /// </summary>
        public static Texture2D CaptureCamera(Camera renderCamera)
        {
            var rt = RenderTexture.active;
            var texture = renderCamera.targetTexture;
            var renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
            Graphics.Blit(texture, renderTexture);
            
            RenderTexture.active = renderTexture;
            var texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
            texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
            texture2D.Apply();
            RenderTexture.active = rt;
            
            RenderTexture.ReleaseTemporary(renderTexture);
            return texture2D;
        }
        
        /// <summary>
        /// 对指定材质进行渲染并返回渲染结果
        /// </summary>
        /// <param name="source">传入的贴图，对应Shader中名为_MainTex的贴图，如果Shader中没有名为_MainTex的贴图则传入null即可。</param>
        public static Texture2D RenderMaterial(Material material, int width, int height, Texture source = null)
        {
            var rt = RenderTexture.GetTemporary(width, height, 0);
            Graphics.Blit(source, rt, material);
            var texture = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false);
            RenderTexture.active = rt;
            texture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            texture.Apply();
            RenderTexture.ReleaseTemporary(rt);
            return texture;
        }
        
        /// <summary>
        /// 缩放贴图
        /// </summary>
        public static Texture2D Scale(Texture2D source, int targetWidth, int targetHeight, TextureFormat textureFormat = TextureFormat.RGBA32)
        {
            var result = new Texture2D(targetWidth, targetHeight, textureFormat, false);
            for (var i = 0; i < result.height; i++)
            {
                for (var j = 0; j < result.width; j++)
                {
                    var newColor = source.GetPixelBilinear((float) j / (float) result.width, (float) i / (float) result.height);
                    result.SetPixel(j, i, newColor);
                }
            }
            result.Apply();
            return result;
        }
        
        /// <summary>
        /// 水平翻转
        /// </summary>
        public static Texture2D HorizontalFlipTexture(Texture2D texture)
        {
            var width = texture.width;
            var height = texture.height;
            var result = new Texture2D(width, height, texture.format, false);
            for (var i = 0; i < width; i++)
            {
                result.SetPixels(i, 0, 1, height, texture.GetPixels(width - i - 1, 0, 1, height));
            }
            result.Apply();
            return result;
        }

        /// <summary>
        /// 垂直翻转
        /// </summary>
        public static Texture2D VerticalFlipTexture(Texture2D texture)
        {
            var width = texture.width;
            var height = texture.height;
            var result = new Texture2D(width, height, texture.format, false);
            for (var i = 0; i < height; i++)
            {
                result.SetPixels(0, i, width, 1, texture.GetPixels(0, height - i - 1, width, 1));
            }
            result.Apply();
            return result;
        }

        /// <summary>
        /// 图片顺时针旋转90度
        /// </summary>
        public static Texture2D RotationRight90(Texture2D texture)
        {
            var width = texture.width;
            var height = texture.height;
            var result = new Texture2D(height, width, texture.format, false);
            var sourceColors = texture.GetPixels();
            var newColors = new Color[sourceColors.Length];
            for (var i = 0; i < height; i++)
            {
                for (var j = 0; j < width; j++)
                {
                    newColors[(width - j - 1) * height + i] = sourceColors[i * width + j];
                }
            }
            result.SetPixels(newColors);
            result.Apply();
            return result;
        }
        
        /// <summary>
        /// 图片逆时针旋转90度
        /// </summary>
        public static Texture2D RotationLeft90(Texture2D texture)
        {
            var width = texture.width;
            var height = texture.height;
            var result = new Texture2D(height, width, texture.format, false);
            var sourceColors = texture.GetPixels();
            var newColors = new Color[sourceColors.Length];
            for (var i = 0; i < width; i++)
            {
                for (var j = 0; j < height; j++)
                {
                    newColors[i * height + j] = sourceColors[(height - 1 - j) * width + i];
                }
            }
            result.SetPixels(newColors);
            result.Apply();
            return result;
        }
    }
}
